package com.zhuoyue.biz.core.command.cmd.common.faa;

import com.zhuoyue.biz.core.api.domain.DeviceAndGatewayDO;
import com.zhuoyue.biz.core.api.domain.TblDeviceActions;
import com.zhuoyue.biz.core.api.dto.req.ControlReq;
import com.zhuoyue.biz.core.command.constant.DuoCaiDeng0Faa0B02CmdDeal;
import com.zhuoyue.common.constant.ControlType;
import com.zhuoyue.common.exception.ErrorType;
import com.zhuoyue.common.exception.ZyServerException;
import com.zhuoyue.common.utils.CmdUtils;
import org.apache.commons.collections.CollectionUtils;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * @author zhanghui
 */
public class OneColorTemperature0Faa0D03 {
    /**
     * 总亮度实际3000
     */
    private static Integer totalBrightness = 30;
    private static boolean isGroupAll = false;
    private static final String DURATION = "duration";
    private static final String MODE_INDEX = "modeIndex";
    private static final int ONE_HUNDRED = 100;

    public static String control(DeviceAndGatewayDO deviceAndGatewayDO, ControlReq controlReq, List<TblDeviceActions> deviceActions, boolean isGroup) throws ParseException {
        isGroupAll = isGroup;
        String controlType = controlReq.getControlType();
        String gatewayMac = deviceAndGatewayDO.getGatewayMac();
        String deviceMac = deviceAndGatewayDO.getZigbeeMac();
        Integer delayTimeInt = 0;
        Integer setColorTemperatureStr = 1500;
        Integer setBrightnessStr = 1500;
        if (CollectionUtils.isNotEmpty(deviceActions)) {
            for (TblDeviceActions action : deviceActions) {
                if ("setBrightness".equals(action.getActionsKey())) {
                    setBrightnessStr = Integer.parseInt(action.getActionsValue());
                }
                if ("setColorTemperature".equals(action.getActionsKey())) {
                    setColorTemperatureStr = Integer.parseInt(action.getActionsValue());
                }
            }
        }
        Map<String, Object> actions = null;
        String duration = "0000";
        String delayTime = "0000";
        int modeIndex = 0;
        Integer setColorTemperature = setColorTemperatureStr;
        Integer setBrightness = setBrightnessStr;
        if (controlReq.getActions() != null) {
            actions = controlReq.getActions();

            if (actions.get(DURATION) == null) {
                throw new ZyServerException(ErrorType.INVALID_PARAMS.getCode(), "actions中duration参数不能为空");
            }

            duration = CmdUtils.getHighPositionReplacement(CmdUtils.getHexString(Integer.parseInt(actions.get("duration") + "")));

            delayTime = actions.get("delayTime") != null ? CmdUtils.getHighPositionReplacement(CmdUtils.getHexString(Integer.parseInt(actions.get("delayTime") + "")))
                    : CmdUtils.getHighPositionReplacement(CmdUtils.getHexString((delayTimeInt)));

            if (actions.containsKey(MODE_INDEX)) {
                modeIndex = Integer.parseInt(actions.get("modeIndex").toString());
            }

            setColorTemperature = actions.get("setColorTemperature") != null ? Integer.parseInt(actions.get("setColorTemperature") + "") : setColorTemperatureStr;
            if (setColorTemperature == 0) {
                setColorTemperature = 1;
            }
            setBrightness = actions.get("setBrightness") != null ? Integer.parseInt(actions.get("setBrightness") + "") : setBrightnessStr;
        }


        String cmd = "";
        switch (controlType) {
            //开灯
            case ControlType.TURN_ON_REQUEST:
                cmd = setOpenControlCmd(gatewayMac, deviceMac, duration, delayTime, dealBrightness(setBrightness), dealColorTemperature(setColorTemperature));
                break;
            //关灯
            case "TurnOffRequest":
                cmd = setCloseControlCmd(gatewayMac, deviceMac, duration, delayTime, dealBrightness(setBrightness), dealColorTemperature(setColorTemperature));
                break;
            //控制色温+亮度
            case "SetBrightnessAndColorTemperaturePercentageRequest":
                cmd = setBrightnessAndColorTemperaturePercentage(dealBrightness(setBrightness), dealColorTemperature(setColorTemperature), duration, delayTime, gatewayMac, deviceMac);
                break;
            //只设置灯亮度
            case "SetBrightnessPercentageRequest":
                cmd = setBrightnessPercentage(dealBrightness(setBrightness), duration, delayTime, gatewayMac, deviceMac);
                break;
            //只设置色温
            case "SetColorTemperaturePercentageRequest":
                cmd = setColorTemperaturePercentage(dealBrightness(setColorTemperature), duration, delayTime, gatewayMac, deviceMac);
                break;
            //设置状态(三种状态,冷 暖 灭)
            case "SetStatusRequest":
                cmd = setStatus(dealBrightness(setBrightness), dealColorTemperature(setColorTemperature), gatewayMac, deviceMac);
                break;
            //立即亮一段时间再灭
            case "OnToOffRequest":
                cmd = setOnToOffOrOfftoOn("07", duration, delayTime, gatewayMac, deviceMac, dealBrightness(setBrightness), dealColorTemperature(setColorTemperature));
                break;
            //立即灭一段时间再亮
            case "OffToOnRequest":
                cmd = setOnToOffOrOfftoOn("08", duration, delayTime, gatewayMac, deviceMac, dealBrightness(setBrightness), dealColorTemperature(setColorTemperature));
                break;
            //取消延时
            case "CancelDelayRequest":
                cmd = setCancelDelay(gatewayMac, deviceMac, duration, delayTime, dealBrightness(setBrightness), dealColorTemperature(setColorTemperature));
                break;
            //设置模式
            case "SetModeRequest":
                cmd = setMode(gatewayMac, deviceMac, Integer.parseInt(actions.get("duration").toString()), modeIndex);
                break;
            //停止模式
            case "StopModeRequest":
                cmd = stopMode(gatewayMac, deviceMac);
                break;
            default:
                break;
        }
        return cmd;
    }

    public static String setCancelDelay(String gatewayMac, String deviceMac, String duration, String delayTime, String nowBrightnessStr, String nowColorTemperatureStr) {
        String cmd10To12 = "1F000000" + "0D" + duration + delayTime + "00000000" + nowBrightnessStr + nowColorTemperatureStr;
        return cmdData(cmd10To12, gatewayMac, deviceMac);
    }

    private static String setMode(String gatewayMac, String deviceMac, Integer duration, Integer modeIndex) {
        String cmd10To12 = "1F000000" + "0E" + modeIndex + duration + "00" + "00000000000000000000";
        return cmdData(cmd10To12, gatewayMac, deviceMac);
    }

    private static String stopMode(String gatewayMac, String deviceMac) {
        String cmd10To12 = "1F000000" + "0F" + "000000000000000000000000";
        return cmdData(cmd10To12, gatewayMac, deviceMac);
    }


    /**
     * 打开
     *
     * @param gatewayMac
     * @param deviceMac
     * @return
     */
    private static String setOpenControlCmd(String gatewayMac, String deviceMac, String duration, String delayTime, String nowBrightnessStr, String nowColorTemperatureStr) {
        String cmd10To12 = "1F000000" + "01" + duration + delayTime + "00000000" + nowBrightnessStr + nowColorTemperatureStr;
        return cmdData(cmd10To12, gatewayMac, deviceMac);
    }


    /**
     * 获取关灯控制命令
     *
     * @param gatewayMac
     * @param deviceMac
     * @return
     */
    private static String setCloseControlCmd(String gatewayMac, String deviceMac, String duration, String delayTime, String nowBrightnessStr, String nowColorTemperatureStr) {
        String cmd10To12 = "1F000000" + "02" + duration + delayTime + "00000000" + nowBrightnessStr + nowColorTemperatureStr;
        return cmdData(cmd10To12, gatewayMac, deviceMac);
    }


    /**
     * 色温+亮度
     *
     * @param dealBrightness
     * @param colorTemperature
     * @return
     */
    private static String setBrightnessAndColorTemperaturePercentage(String dealBrightness, String colorTemperature, String duration, String delayTime, String gatewayMac, String deviceMac) {
        String cmd10To12 = "1F000000" + "03" + duration + delayTime + "00000000" + dealBrightness + colorTemperature;
        return cmdData(cmd10To12, gatewayMac, deviceMac);
    }

    /**
     * 亮度
     *
     * @param brightnessInt
     * @return
     */
    private static String setBrightnessPercentage(String brightnessInt, String duration, String delayTime, String gatewayMac, String deviceMac) {
        String cmd10To12 = "1F000000" + "04" + duration + delayTime + "00000000" + brightnessInt + "0000";
        return cmdData(cmd10To12, gatewayMac, deviceMac);
    }

    /**
     * 色温
     *
     * @param colorTemperature
     * @param gatewayMac
     * @param deviceMac
     * @return
     */
    private static String setColorTemperaturePercentage(String colorTemperature, String duration, String delayTime, String gatewayMac, String deviceMac) {
        String cmd10To12 = "1F000000" + "05" + duration + delayTime + "000000000000" + colorTemperature;
        return cmdData(cmd10To12, gatewayMac, deviceMac);
    }

    private static String setStatus(String dealBrightness, String colorTemperature, String gatewayMac, String deviceMac) {
        String cmd10To12 = "1F000000" + "06" + "0000000000000000" + dealBrightness + colorTemperature;
        return cmdData(cmd10To12, gatewayMac, deviceMac);
    }

    /**
     * 立即亮一段时间再灭
     * 立即灭一段时间再亮
     *
     * @param duration   亮度保持时间
     * @param delayTime  延时时间
     * @param gatewayMac
     * @param deviceMac
     * @return
     */
    private static String setOnToOffOrOfftoOn(String mode, String duration, String delayTime, String gatewayMac, String deviceMac, String nowColorTemperatureStr, String nowBrightnessStr) {
        String cmd10To12 = "1F000000" + mode + duration + delayTime + "00000000" + nowColorTemperatureStr + nowBrightnessStr;
        return cmdData(cmd10To12, gatewayMac, deviceMac);
    }

    private static String cmdData(String cmd10To12, String gatewayMac, String deviceMac) {
        String cmd9 = CmdUtils.getDateLength(cmd10To12);
        String cmd4 = "";
        if (isGroupAll) {
            cmd4 = "02";
        } else {
            cmd4 = "01";
        }
        String cmd3To12 = DuoCaiDeng0Faa0B02CmdDeal.CloseControl.CMD_3 +
                cmd4 +
                gatewayMac + deviceMac +
                DuoCaiDeng0Faa0B02CmdDeal.CloseControl.CMD_7 +
                DuoCaiDeng0Faa0B02CmdDeal.CloseControl.CMD_8 +
                cmd9 + cmd10To12;

        String cmd2 = CmdUtils.getDateLength(cmd3To12);

        String cmd13 = CmdUtils.getCmdBodySum(cmd3To12);

        String fullCmd = DuoCaiDeng0Faa0B02CmdDeal.CloseControl.CMD_1 +
                cmd2 + cmd3To12 + cmd13 +
                DuoCaiDeng0Faa0B02CmdDeal.CloseControl.CMD_14;
        return fullCmd;
    }


    private static String dealBrightness(Integer doubleBrightness) {
        if (doubleBrightness > ONE_HUNDRED) {
            doubleBrightness = 100;
        }
        //计算亮度
        String setBrightness = totalBrightness * doubleBrightness + "";
        int brightnessInt = Integer.parseInt(setBrightness);
        String br1 = CmdUtils.getHexString(brightnessInt);
        String sub = CmdUtils.getHighPositionReplacement(br1);
        return sub;
    }

    private static String dealColorTemperature(Integer setColorTemperature) {
        if (setColorTemperature > ONE_HUNDRED) {
            setColorTemperature = 100;
        }
        //计算色温
        Integer colorTemperatureInt = totalBrightness * setColorTemperature;
        String br1 = CmdUtils.getHexString(colorTemperatureInt);
        String sub = CmdUtils.getHighPositionReplacement(br1);
        return sub;
    }
}
